﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;

namespace SHKF.B2C.Mannage.AdminUI.Controls
{
    public class ProductBaseImage : Control
    {
        public event RoutedEventHandler ClickAdd;
        public event RoutedEventHandler ClickDelete;
        private Image image;

        public override void OnApplyTemplate()
        {
            Button btnAdd = (Button)this.Template.FindName("btnAdd", this);
            if (btnAdd != null)
            {
                btnAdd.Click += btnAdd_Click;
            }

            Button btnDelete = (Button)this.Template.FindName("btnDelete",this);
            if (btnDelete != null)
            {
                btnDelete.Click += btnDelete_Click;
            }

            image = (Image)this.Template.FindName("image", this);
            
        }


        public ImageSource GetImageSource()
        {
            return this.image.Source;
        }


        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }
         
        public static readonly DependencyProperty TitleProperty =
            DependencyProperty.Register("Title", typeof(string), typeof(ProductBaseImage), new PropertyMetadata(""));
         
        public int Index { get; set; }

        private void btnAdd_Click(object sender, RoutedEventArgs e)
        {
            if (this.DataContext == null)
            {
                if (ClickAdd != null)
                {
                    ClickAdd(this, e);
                }
            }
            else
            {
                MessageBox.Show("请先删除图片");
            }
        }

        private void btnDelete_Click(object sender, RoutedEventArgs e)
        {
            if (this.DataContext != null)
            {
                if (ClickDelete != null)
                {
                    ClickDelete(this, e);
                }
            }
        }
    }
}
